// Blob Buffer
// (c) jimon game studio

#include "CBlobBuffer.h"

// blob dont need any allocation methods, base buffer idea is different between enesea and sqlite

namespace je
{
	namespace core
	{
		//! Constructor
		CBlobBuffer::CBlobBuffer(sqlite3_blob * OpenBlob)
			:Blob(OpenBlob),Locked(false),Position(0)
		{
			Size = sqlite3_blob_bytes(Blob);
		}

		//! Destructor
		CBlobBuffer::~CBlobBuffer()
		{
			sqlite3_blob_close(Blob);
		}

		//! Read Data
		void CBlobBuffer::Read(void * Pointer,u32 Bytes)
		{
			JEASSERT(Locked)
			JEASSERT((LockMode == BLT_READ) || (LockMode == BLT_READWRITE))

			sqlite3_blob_read(Blob,Pointer,Bytes,Position);

			if(StreamWorkMode)
				Position += Bytes;
		}

		//! Write Data
		void CBlobBuffer::Write(const void * Pointer,u32 Bytes)
		{
			JEASSERT(Locked)
			JEASSERT((LockMode == BLT_WRITE) || (LockMode == BLT_READWRITE))

			sqlite3_blob_write(Blob,Pointer,Bytes,Position);

			if(StreamWorkMode)
				Position += Bytes;
		}

		//! Allocate Buffer for data,size is count of objects
		void CBlobBuffer::Allocate(u32 NewSize)
		{
			Size = NewSize;
		}

		//! DeAllocate Buffer
		void CBlobBuffer::DeAllocate()
		{
			Size = 0;
		}
	}
}
